//
//  NewFeatureView.swift
//  Weibo
//
//  Created by 唐润欣 on 16/6/18.
//  Copyright © 2016年 唐润欣. All rights reserved.
//

import UIKit

// MARK: - 属性、构造函数、生命周期函数
class NewFeatureView: UIView {

    private lazy var scrollView: UIScrollView = {
        
        let scrollView = UIScrollView()
        
        scrollView.bounces = false
        scrollView.pagingEnabled = true
        scrollView.showsHorizontalScrollIndicator = false
        scrollView.delegate = self
        
        return scrollView
    }()
    
    private lazy var pageControl: UIPageControl = {
        
        let pageControl = UIPageControl()
        
        pageControl.numberOfPages = 4
        pageControl.currentPage = 0
        pageControl.userInteractionEnabled = false
        pageControl.currentPageIndicatorTintColor = globalColor
        pageControl.pageIndicatorTintColor = UIColor.blackColor()
        
        return pageControl
    }()
    
    init() {
        
        super.init(frame: screenBounds)
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

extension NewFeatureView {
    
    func setupUI() {
        
        //1. 添加子控件
        addSubview(scrollView)
        addSubview(pageControl)
        
        //自动布局
        //a. 添加scrollView的子view
        for i in 1...4 {
            
            let imageView = UIImageView(imageName: "new_feature_\(i)")
            
            scrollView.addSubview(imageView)
            imageView.snp_makeConstraints(closure: { (make) -> Void in
                
                let offset = CGFloat(i - 1) * screenWidth
                
                make.top.equalTo(scrollView)
                make.left.equalTo(scrollView).offset(offset)
                make.size.equalTo(screenBounds.size)
            })
        }
        
        //b. scrollView自动布局
        //在scrollView自动布局时，bottom和right确定的时scrollView的contentSize
        //设定scroll的bottom和right需要参照scrollView的子view
        //scrollView的size一定要设
        scrollView.snp_makeConstraints { (make) -> Void in
            
            make.top.equalTo(self)
            make.left.equalTo(self)
            make.size.equalTo(self)
            
            let lastImageView = scrollView.subviews.last!
            make.right.equalTo(lastImageView).offset(screenWidth)
        }
        
        //c.PageControl的自动布局
        pageControl.snp_makeConstraints { (make) -> Void in
            
            make.centerX.equalTo(self)
            make.centerY.equalTo(self).offset(240)
        }
    }
}

extension NewFeatureView: UIScrollViewDelegate {
    
    //只要scrollView在滚动，都会调用
    //可以比较准备地监听contentOffset
    func scrollViewDidScroll(scrollView: UIScrollView) {
        
        //当第四张图片滚出scrollView的时候（滚出四个screenWidth的时候），移除 新功能页
        let contentOffset = scrollView.contentOffset.x
        
        if contentOffset >= 4 * screenWidth {
            
            self.removeFromSuperview()
        }
        
        //当scrollView滚动到screenWidth的二分之一以上，就改变pageControl的currentPage的值
        //四舍五入算法：
        let currentPage = Int(contentOffset / screenWidth + 0.5)
        pageControl.currentPage = currentPage
        pageControl.hidden = contentOffset > 3 * screenWidth
    }
}
